include("shared.lua");
include("libraries/sh_class.lua");
include("config.lua");
include("libraries/cl_util.lua");
include("libraries/sh_player.lua");
include("libraries/panels/scoreboard.lua");

function GM:Initialize()
	surface.CreateFont("coolvetica", 42, 500, true, false, "lgtrp_scoreboardHeader", false, true);
end;

function GM:OnEntityCreated(entity)
	if (IsValid(entity) and entity == LocalPlayer()) then
		RunConsoleCommand("lgtrp_loadplayer");
	end;
end;

function GM:HUDPaint()
	self.BaseClass:HUDPaint();
	
	local maxWidth = math.Clamp(LocalPlayer():Health() /100 *220, 0, 220);
	
	draw.RoundedBox(4, 5, ScrH() -17, 220, 11, Color(0, 0, 0, 240));
	draw.DrawSimpleRect(6, ScrH() -16, maxWidth -2, 9, Color(180, 0, 0, 255));
	
	if (LocalPlayer():Armor() > 0) then
		local maxWidth = math.Clamp(LocalPlayer():Armor() /100 *220, 0, 220);
		
		draw.RoundedBox(4, 5, ScrH() -32, 220, 11, Color(0, 0, 0, 240));
		draw.DrawSimpleRect(6, ScrH() -31, maxWidth -2, 9, Color(0, 0, 180, 255));
	end;
end;

local lastHolster = true;

function GM:Think()
	local weapon = LocalPlayer():GetActiveWeapon();
	local holstered = LocalPlayer():GetNWBool("lgtrp_holstered");
	
	if (weapon and weapon:IsValid() and weapon.Primary and weapon.PositionTime) then
		if (lastHolster != holstered) then
			if (holstered) then
				weapon.PositionMode = 2;
				weapon.PositionTime = .3;
				weapon.PositionMul = 1;
			else
				weapon.PositionMode = 3;
				weapon.PositionTime = .3;
				weapon.PositionMul = 0;
			end;

			lastHolster = holstered;
		end;
		
		if (LocalPlayer():KeyDown(IN_ATTACK2) and holstered) then
			weapon.PositionMode = 1;
			weapon.PositionMul = math.Clamp(weapon.PositionMul +(1 /weapon.PositionTime) *FrameTime(), 0, 1);
		elseif (weapon.PositionMode == 1) then
			weapon.PositionMul = math.Clamp(weapon.PositionMul -(1 /weapon.PositionTime) *FrameTime(), 0, 1);
			
			if (weapon.PositionMul == 0) then
				weapon.PositionMode = 0;
			end;
		end;
		
		-- While we're unholstering it.
		if (weapon.PositionMode == 2) then
			if (weapon.HolsteredPos) then
				weapon.PositionMul = math.Clamp(weapon.PositionMul -(1 /weapon.PositionTime) *FrameTime(), 0, 1);
				
				if (weapon.PositionMul == 0) then
					weapon.PositionMode = 0;
				end;
			end;
		end;
		
		-- While we're holstering it.
		if (weapon.PositionMode == 3) then
			if (weapon.HolsteredPos) then
				weapon.PositionMul = math.Clamp(weapon.PositionMul +(1 /weapon.PositionTime) *FrameTime(), 0, 1);
			end;
		end;
	end;
end;


local defaultHUD = {
	"CHudHealth",
	"CHudBattery"
	--"CHudAmmo",
	--"CHudSecondaryAmmo"
};

function GM:HUDShouldDraw(hudID) return !table.HasValue(defaultHUD, hudID); end;
function GM:HUDDrawTargetID() end;

usermessage.Hook("leaf_icchat", function(um)
	local name = um:ReadString()
	local text = um:ReadString();

	chat.AddText(Color(151, 211, 255, 255), name .. ": " .. text);
end);

usermessage.Hook("leaf_wchat", function(um)
	local name = um:ReadString()
	local text = um:ReadString();

	chat.AddText(Color(151, 211, 255, 255), "[Whisper] " .. name .. ": " .. text);
end);

usermessage.Hook("leaf_ychat", function(um)
	local name = um:ReadString()
	local text = um:ReadString();

	chat.AddText(Color(151, 211, 255, 255), "[Yell] " .. name .. ": " .. text);
end);

usermessage.Hook("leaf_loocchat", function(um)
	local name = um:ReadString()
	local text = um:ReadString();

	chat.AddText(Color(151, 211, 255, 255), "[Local-OOC] " .. name .. ": " .. text);
end);

usermessage.Hook("leaf_oochat", function(um)
	local id = um:ReadLong()
	local text = um:ReadString();
	local player = ents.GetByIndex(id);
	
	if (IsValid(player)) then
		local name = player:Nick();
		local color = team.GetColor(player:Team());
		
		chat.AddText(color_white, "[OOC] ", color, name, color_white, ": " .. text);
	end;
end);